//
//  ViewController.swift
//  UploadImg(swift)
//
//  Created by 郑东喜 on 2016/11/15.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
//        let str = "http://yungou.ie1e.com/ifs/upphoto.ashx?token=0510523517"
//        let url = URL.init(string: str)
//        
//        //1.创建可变请求
//        let request = NSMutableURLRequest.init(url: url!)
//        
//        //2.创建请求数据
//        let image = UIImage.init(named: "ico.jpg")
//        let imageData = UIImageJPEGRepresentation(image!, 0.1)
//        
//        //3.设置上传
//        request.httpBody = imageData
//        request.httpMethod = "POST"
//        
//        //4.文件上传
//        
//        NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue:OperationQueue()) { (res, data, error)in
//            //返回主线程执行
//            DispatchQueue.main.sync {
//                
//                if let data = data {
//                    
//                    if let result = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) {
//                        print(result)
//                        
//                    }
//                    
//                }else {
//                    print(error?.localizedDescription as Any)
//                }
//                
//            }
//
//        }
        
        let str = "http://yungou.ie1e.com/ifs/upphoto.ashx?token=0510523517"
        
        
        //2.创建请求数据
        let image = UIImage.init(named: "ico.jpg")
        let imageData = UIImageJPEGRepresentation(image!, 0.1)
        
        postWithImageWithData(imgData: imageData!, path: str, success: { (response) in
            print(response)
        }) { (error) in
            print(error.localizedDescription)
        }
    }
    
    ///上传图片
    func postWithImageWithData(imgData : Data? ,path: String,success: @escaping ((_ result: Any) -> ()),failure: @escaping ((_ error: Error) -> ())) {
        
        
        //(1）设置请求路径
        let url:NSURL = NSURL(string:path)!//不需要传递参数
        
        //1.创建可变请求
        let request = NSMutableURLRequest.init(url: url as URL)

        
        //2.设置上传
        request.httpBody = imgData
        request.timeoutInterval = 5.0 //设置请求超时为5秒
        request.httpMethod = "POST"  //设置请求方法
        
        
        //(3) 发送请求
        NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue:OperationQueue()) { (res, data, error)in
            //返回主线程执行
            DispatchQueue.main.sync {
                
                if let data = data {
                    
                    if let result = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) {
                        
                        success(result)
                    }
                    
                }else {
                    failure(error!)
                }
                
            }
        }
        
        
    
    
    }


}

